﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Net;

namespace Fotobanka
{
    public partial class PridatFotografii : System.Web.UI.Page
    {
        PhotoManager pm;
        protected void Page_Load(object sender, EventArgs e)
        {
            pm = new PhotoManager();
        }

        protected void SubmitFile_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                    if (FileUpload1.PostedFile.ContentLength < 4194304) //4mb - 4096kb
                    {
                        try
                        {
                            switch (FileUpload1.PostedFile.ContentType)
                            {
                                case "image/jpeg":
                                    pm.InsertPhoto(Convert.ToInt32(Request.QueryString["id"]), FileUpload1, title.Text);
                                    errors.Text = "Soubor nahrán na server";
                                    break;
                                case "application/zip":
                                    {
                                        ZpracujZip(FileUpload1.PostedFile.InputStream);
                                        errors.Text = "Soubor nahrán na server";
                                    }               
                                    break;
                            }
                              
                        }
                        catch (System.Exception exc)
                        {
                            Console.WriteLine(exc.Message);
                        }

                    }
               

            } else {
                errors.Text = "Není vybrána fotografie";
            }
        }

        private void ZpracujZip(Stream input)
        {
            ZipInputStream s = new ZipInputStream(input);
            ZipEntry theEntry;
            string tmpEntry = String.Empty;

            while ((theEntry = s.GetNextEntry()) != null)
            {
                string fileName = Path.GetFileName(theEntry.Name);

                if (fileName != String.Empty)
                {
                    if (theEntry.Name.IndexOf(".ini") < 0 && theEntry.Name.IndexOf(".jpg") >= 0)
                    {
                        MemoryStream ms = new MemoryStream();

                        int size;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                ms.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }

                        pm.InsertPhoto(Convert.ToInt32(Request.QueryString["id"]), ms, title.Text, fileName);
                        ms.Close();
                    }
                }
            }

            s.Close();
        }

        protected void url_Click(object sender, EventArgs e)
        {
            if (url.Text == "")
            {
                errors2.Text = "Není zadána url";
            } /*else if (url.Text.IndexOf("ftp://") < 0)
            {
                errors2.Text = "Url není adresována na ftp";
            }*/ else if (url.Text.IndexOf(".zip") < 0)
            {
                errors2.Text = "Url neodkazuje na zip soubor";
            } else {
                WebClient client = new WebClient();
                
                byte[] file = client.DownloadData(url.Text);
                MemoryStream ms = new MemoryStream(file);
                ZpracujZip(ms);
                ms.Close();

                url.Text = "";
                errors2.Text = "Soubor nahrán na server";
            }
            
        }
    }
}
